unit uTileOptions;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls,
  uChooseTile;

type
  TGameTileOptions = class(TForm)
    TileSetImage: TImage;
    WaterCheck: TCheckBox;
    GorgeCheck: TCheckBox;
    GhostyCheck: TCheckBox;
    TransparentCheck: TCheckBox;
    YPosEdit: TEdit;
    XPosEdit: TEdit;
    TileSetCombo: TComboBox;
    OKButton: TButton;
    CancelButton: TButton;
    procedure onObjectClick(Sender: TObject);
    procedure onShowIt(Sender: TObject);
    procedure FormCanResize(Sender: TObject; var NewWidth, NewHeight: Integer;
      var Resize: Boolean);
    procedure TileSetComboChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    useTile: TGameChooseTile;
  end;

var
  GameTileOptions: TGameTileOptions;

implementation

{$R *.dfm}

uses
  uTileSet, uMapEditor;

procedure TGameTileOptions.FormCanResize(Sender: TObject; var NewWidth,
  NewHeight: Integer; var Resize: Boolean);
begin
  Resize := false;
end;

procedure TGameTileOptions.FormCreate(Sender: TObject);
begin
  self.DoubleBuffered := true;
end;

procedure TGameTileOptions.onObjectClick(Sender: TObject);
var
  i: Integer;
begin
  if (Sender is TButton) then
    begin
      if ((Sender as TButton).Caption = 'OK') then
        begin
          useTile.tile.isWater := self.WaterCheck.Checked;
          useTile.tile.isGorge := self.GorgeCheck.Checked;
          useTile.tile.isGhosty := self.GhostyCheck.Checked;
          useTile.tile.tileTexture := (StrToInt(self.YPosEdit.Text)*6) + StrToInt(self.XPosEdit.Text);
          useTile.tile.transparent := self.TransparentCheck.Checked;
          for i := low(GameMapEditor.screen.useMap.tileSets) to high(GameMapEditor.screen.useMap.tileSets) do
            begin
              if (self.TileSetCombo.Text = GameMapEditor.screen.useMap.tileSets[i].name) then
                begin
                  useTile.tile.tileSet := GameMapEditor.screen.useMap.tileSets[i];
                end;
            end;
          GameMapEditor.choosed := useTile.tile;
          useTile.Picture.Graphic := useTile.tile.tileSet;
          self.Hide;
        end;
    end;
end;

procedure TGameTileOptions.onShowIt(Sender: TObject);
var
  i, j: Integer;
begin
  self.TileSetCombo.Items.Clear;
  for i := low(GameMapEditor.screen.useMap.tileSets) to high(GameMapEditor.screen.useMap.tileSets) do
    begin
      self.TileSetCombo.Items.Add(GameMapEditor.screen.useMap.tileSets[i].name);
    end;
  self.TileSetImage.Canvas.Draw(0, 0, nil);
  self.TileSetImage.Canvas.Draw(0, 0, self.useTile.tile.tileSet);
  self.WaterCheck.Checked := self.useTile.tile.isWater;
  self.GorgeCheck.Checked := self.useTile.tile.isGorge;
  self.GhostyCheck.Checked := self.useTile.tile.isGhosty;
  self.TransparentCheck.Checked := self.useTile.tile.transparent;
  i := self.useTile.tile.tileTexture;
  while ((i > 5) and (j < 3)) do
    begin
      j := j + 1;
      i := i - 6;
    end;
  self.XPosEdit.Text := IntToStr(i);
  self.YPosEdit.Text := IntToStr(j);
  self.TileSetCombo.Text := self.useTile.tile.tileSet.name;
end;

procedure TGameTileOptions.TileSetComboChange(Sender: TObject);
var
  i, j: Integer;
begin
  for i := low(GameMapEditor.screen.useMap.tileSets) to high(GameMapEditor.screen.useMap.tileSets) do
    begin
      if (self.TileSetCombo.Text = GameMapEditor.screen.useMap.tileSets[i].name) then
        begin
          self.TileSetImage.Canvas.Draw(0, 0, nil);
          self.TileSetImage.Canvas.Draw(0, 0, GameMapEditor.screen.useMap.tileSets[i]);
        end;
    end;
end;

end.
